package ui.fragment.minefragment;

import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zzhoujay.richtext.RichText;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.MyPointsAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.MyPointBean;
import bean.MyPointDetailBean;
import student.andlei.com.student.R;
import ui.fragment.MineFragment;
import utils.GlideUtils;
import utils.GsonUtils;
import utils.log.LogUtil;

/**
 * 我的积分
 */
public class MyPointsFragment extends BaseNetFragment{
    private TextView tv_rule,
                     tv_detail,
                    tv_mypoint ;
    private TextView tv_paiminng,
                     tv_name,
                     tv_score;
    private ImageView img_headimg;
    private ScrollView scrollView;
    private RecyclerView recyclerView;
    private List<MyPointDetailBean.My_rank> lists = new ArrayList<>();
    private MyPointsAdapter adapter;
    private MyPointDetailBean pointDetailBean;
    public static Fragment getInstance() {
        return new MyPointsFragment();
    }
    @Override
    public void success(String action, BaseBean baseBean) {
            if(baseBean.code == 200){
                pointDetailBean = (MyPointDetailBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,MyPointDetailBean.class);
                lists = pointDetailBean.ranks;
                // tv_mypoint.setText("我的积分："+pointDetailBean.score);
                String str = "我的积分："+"<font font-size=\"150%\";color=\"red\">"+pointDetailBean.score+"</font>";
                tv_mypoint.setText(Html.fromHtml(str));
                tv_score.setText(pointDetailBean.my_rank.score+"");
                tv_name.setText(pointDetailBean.my_rank.student_name);
                tv_paiminng.setText(pointDetailBean.my_rank.rownum+"");
                GlideUtils.loadImageViewHeadphotoLoding(context,pointDetailBean.my_rank.head_image,img_headimg);
                adapter.setNewData(lists);
            }else {
                toast(baseBean.msg);
            }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mypoint;
    }

    @Override
    protected void findViews() {
        scrollView = getView(R.id.scrollview);
        tv_mypoint = getView(R.id.tv_mypoint_mypointnumber);
        tv_detail = getView(R.id.tv_mypoint_mypointdetail);
        tv_paiminng = getView(R.id.tv_ranking);
        tv_name = getView(R.id.tv_name);
        tv_score = getView(R.id.tv_points);
        img_headimg = getView(R.id.civ_logo);
        tv_rule = getView(R.id.tv_rule);
        recyclerView = getView(R.id.ry_my);
    }

    @Override
    protected void formatViews() {
        tv_detail.setOnClickListener(this);
        tv_rule.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
//        for (int i=0;i<20;i++){
//            MyPointBean bean = new MyPointBean();
//            bean.setImgurl("http://c.hiphotos.baidu.com/image/pic/item/a5c27d1ed21b0ef4b129b3b9d1c451da80cb3e17.jpg");
//            bean.setName("刘静"+i);
//            bean.setPoints("7"+i);
//            bean.setRank(""+i);
//            lists.add(bean);
//        }

        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //创建适配器
        adapter = new MyPointsAdapter(this.getActivity(),R.layout.item_my_point,lists);

        //给RecyclerView设置适配器
        recyclerView.setAdapter(adapter);
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_UP){
                    //允许ScrollView截断点击事件，ScrollView可滑动
                    scrollView.requestDisallowInterceptTouchEvent(false);
                }else{
                    //不允许ScrollView截断点击事件，点击事件由子View处理
                    scrollView.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });
        loadData();
    }

    @Override
    protected void getBundle() {

    }
    private void loadData(){
        post("personal/myScore",BaseBean.class,false);

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //积分明细
            case R.id.tv_mypoint_mypointdetail:
                ((MineFragment)(this.getParentFragment())).GojumpIntegraldetailfragment();
                break;
            //积分规则
            case R.id.tv_rule:
                ((MineFragment)(this.getParentFragment())).GojumpIntegralRuleFragment();
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            loadData();
        }
    }
}
